﻿<cfsilent>
	<cfflush interval="1024" />
	<cfscript>
		
		imgObj = getProperty("serviceFactory").getBean("imageObject");
		fileObj = getProperty("serviceFactory").getBean("tagFileHelper");
		
		temFile = GetTempDirectory() & createUUID() & ".jpg";
		
		stuId = event.getArg("StudentID");

		portraitDir = getProperty("cdnAssetsPath") & "/student/portrait/" & left( stuId, 4 ) & "/";
		profileDir = getProperty("cdnAssetsPath") & "/student/profile/" & left( stuId, 4 ) & "/";
		printDir = getProperty("cdnAssetsPath") & "/student/print/" & left( stuId, 4 ) & "/";
		
		if ( not directoryExists( portraitDir ) ) {
			directoryCreate( portraitDir );
		}
		if ( not directoryExists( profileDir ) ) {
			directoryCreate( profileDir );
		}
		if ( not directoryExists( printDir ) ) {
			directoryCreate( printDir );
		}

		targetArgs = structNew();
		structInsert(targetArgs, "TabID", "labImport", true);

	</cfscript>
</cfsilent>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>导入学生照片</title>
<link href="<cfoutput>#getProperty('cdnServer')#</cfoutput><cfoutput>#getContextRoot()#</cfoutput>/acadmicRes/assets/css/impoter.css" rel="stylesheet" type="text/css">
</head>

<body>
	
	<cfif isDefined("FORM.upload")>
		
		<!--- 将文件保存至临时目录 --->
		<cftry>
			<cffile action="upload" fileField="upload" destination="#temFile#" accept="image/jpg, image/jpeg" nameconflict="overwrite">
			<cfcatch type="application">
				<p class="error">上传的文件类型不正确, 请使用至少 480x640 尺寸（比例4:3）的 jpg 图片作为上传文件.</p>
			</cfcatch>
		</cftry>
		
		<!--- 将文件转换为对应格式 --->
		<cfif fileExists(temFile)>
			
			<p class="success">保存上传图片 ... 完成</p>
			
			<p class="progress">正在生成用户头像

			<cftry>

				<cfset imgObj.readImage( temFile ) />
				
				<cfset imgObj.scaleWidth( 152 ) />
				<cfset imgObj.blur( 1 ) />
				<cfset imgObj.crop( 1, (imgObj.getHeight()-150)/2, 150, 150 ) />
				<cfset imgObj.scaleWidth( 50 ) />
				<cfset imgObj.writeImage( portraitDir & stuId & ".jpg", "jpg", 100) />
				
				<!--- /* 将图片对象放置到系统缓存 */ --->
				<cfset binaryStream = fileObj.readBinary( portraitDir & stuId & ".jpg" ) />
				<cfset cachePut( "user_portrait_" & stuId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" ) />
				
				... 已完成
				<cfcatch type="any">
					<p class="error">无法生成用户头像, 请检查上传图片比例, 请使用至少 480x640 尺寸（比例4:3）的 jpg 图片作为上传文件</p>
				</cfcatch>
			</cftry>
			
			</p> 
			
			<p class="progress">正在生成用于网页显示的学生照片</p> 
			
			<cftry>
				<cfset imgObj.readImage( temFile ) />

				<cfset imgObj.scaleWidth( 405 ) />
				<cfset imgObj.blur( 1 ) />
				<cfset imgObj.scaleWidth( 137 ) />
				<cfset imgObj.crop( 1, (imgObj.getHeight()-180)/2, 135, 180 ) />
				<cfset imgObj.writeImage( profileDir & stuId & ".jpg", "jpg", 100) />
				
				<!--- /* 将图片对象放置到系统缓存 */ --->
				<cfset binaryStream = fileObj.readBinary( profileDir & stuId & ".jpg" ) />
				<cfset cachePut( "user_profile_" & stuId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" ) />
				
				<cfcatch type="any">
					<p class="error">无法生成网页照片, 请检查上传图片比例, 请使用至少 480x640 尺寸（比例4:3）的 jpg 图片作为上传文件</p>
				</cfcatch>
			</cftry>
			
			<p class="progress">正在生成用于打印证明材料的学生照片</p> 
			
			<cftry>
				<cfset imgObj.readImage( temFile ) />

				<cfset imgObj.scaleWidth( 484 ) />
				<cfset imgObj.crop( 2, (imgObj.getHeight()-640)/2, 480, 640 ) />
				<cfset imgObj.writeImage( printDir & stuId & ".jpg", "jpg", 100) />
				
				<!--- /* 将图片对象放置到系统缓存 */ --->
				<cfset binaryStream = fileObj.readBinary( printDir & stuId & ".jpg" ) />
				<cfset cachePut( "user_print_" & stuId, binaryStream, createTimeSpan(2, 0, 0, 0), createTimeSpan(2, 0, 0, 0), "matrixPicture" ) />
				
				<cfcatch type="any">
					<p class="error">无法生成打印用学生照片, 请检查上传图片比例, 请使用至少 480x640 尺寸（比例4:3）的 jpg 图片作为上传文件</p>
				</cfcatch>
			</cftry>
			
		</cfif>
		
		<!--- 删除临时文件 --->
		<cfif fileExists(temFile)>
			<cffile action="delete" file="#temFile#" />
		</cfif>
		
		<p class="success">上传图片 ... 完成</p>
		
	</cfif>

</body>
</html>